package jia;

import jason.asSemantics.DefaultInternalAction;
import jason.asSemantics.TransitionSystem;
import jason.asSemantics.Unifier;
import jason.asSyntax.NumberTerm;
import jason.asSyntax.NumberTermImpl;
import jason.asSyntax.Term;

import java.util.logging.Level;

public class hospProx extends DefaultInternalAction {

	private static final long serialVersionUID = 1L;

	@Override
	public Object execute(TransitionSystem ts, Unifier un, Term[] terms)
			throws Exception {
		try {
			
			int iagx = (int) ((NumberTerm) terms[0]).solve();
			int iagy = (int) ((NumberTerm) terms[1]).solve();

			int iagxa = (int) ((NumberTerm) terms[2]).solve();
			int iagya = (int) ((NumberTerm) terms[3]).solve();

			int iagxb = (int) ((NumberTerm) terms[4]).solve();
			int iagyb = (int) ((NumberTerm) terms[5]).solve();

			int iagxc = (int) ((NumberTerm) terms[6]).solve();
			int iagyc = (int) ((NumberTerm) terms[7]).solve();

			int a = Math.abs((iagx - iagxa)) + Math.abs((iagy - iagya));
			int b = Math.abs((iagx - iagxb)) + Math.abs((iagy - iagyb));
			int c = Math.abs((iagx - iagxc)) + Math.abs((iagy - iagyc));
			
			int retx = 0;
			int rety = 0;
			
			if(a < b && a < c){
				retx = iagxa;
				rety = iagya;
			}else if(b < a && b < c){
				retx = iagxb;
				rety = iagyb;					
			}else{
				retx = iagxc;
				rety = iagyc;				
			}
			
			un.unifies(terms[8], new NumberTermImpl(retx));
						
			return un.unifies(terms[9], new NumberTermImpl(rety));
		} catch (Throwable e) {
			ts.getLogger().log(Level.SEVERE, "direction error: " + e, e);
		}
		return false;
	}
}
